package com.coocaa.ops.admin.v1.tab.convert;

import com.coocaa.ops.common.core.base.enums.ResourceTypeEnum;
import com.coocaa.ops.common.core.biz.dmp.entity.DmpRegistryRlsInfoEntity;
import com.coocaa.ops.common.core.biz.tab.constant.TabTypeConstants;
import com.coocaa.ops.common.core.biz.tab.entity.TabInfoEntity;
import com.coocaa.ops.common.core.biz.tab.entity.TabPanelConfigInfo;
import com.coocaa.ops.common.core.biz.tab.entity.TabPanelRlsEntity;
import lombok.experimental.UtilityClass;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * @author bijiahao
 * @description
 * @date 2020/6/17 21:42
 */
@UtilityClass
public class TabSaveConverter {

    public static void handleBeforeSave(TabInfoEntity tabInfoEntity) {
        handleDmpFlag(tabInfoEntity);
    }


    public static DmpRegistryRlsInfoEntity getDmpRegistryRlsInfoEntity(TabInfoEntity entity, TabPanelConfigInfo tabPanelConfigInfo) {
        DmpRegistryRlsInfoEntity dmpRegistryRlsInfoEntity = tabPanelConfigInfo.getDmpRegistryInfo();
        dmpRegistryRlsInfoEntity.setParentContainerId(entity.getId());
        dmpRegistryRlsInfoEntity.setParentContainerVersion(entity.getCurrentVersion());
        dmpRegistryRlsInfoEntity.setParentContainerType(ResourceTypeEnum.TAB.getValue());
        dmpRegistryRlsInfoEntity.setSubContainerId(tabPanelConfigInfo.getId());
        dmpRegistryRlsInfoEntity.setSubContainerType(ResourceTypeEnum.PANEL.getValue());
        dmpRegistryRlsInfoEntity.setCreator(entity.getModifier());
        dmpRegistryRlsInfoEntity.setModifier(entity.getModifier());
        return dmpRegistryRlsInfoEntity;
    }


    public static TabPanelRlsEntity getTabPanelRlsEntity(TabInfoEntity entity, TabPanelConfigInfo tabPanelConfigInfo) {
        TabPanelRlsEntity saveRlsEntity = new TabPanelRlsEntity();
        saveRlsEntity.setTabId(entity.getId());
        saveRlsEntity.setTabVersion(entity.getCurrentVersion());
        saveRlsEntity.setPanelId(tabPanelConfigInfo.getId());
        saveRlsEntity.setCreator(entity.getModifier());
        saveRlsEntity.setModifier(entity.getModifier());
        return saveRlsEntity;
    }

    private static void handleDmpFlag(TabInfoEntity entity) {
        if (entity == null) {
            return;
        }
        if (TabTypeConstants.isNormal(entity.getTabType())) {
            List<TabPanelConfigInfo> panelList = entity.getPanelList();
            if (hasDmpForNormal(panelList)) {
                entity.setHasDmp(1);
                return;
            }
        }
        entity.setHasDmp(0);
    }


    private static boolean hasDmpForNormal(List<TabPanelConfigInfo> panelList) {
        if (CollectionUtils.isEmpty(panelList)) {
            return false;
        }
        boolean hasDmp = false;
        for (TabPanelConfigInfo panel : panelList) {
            if (Objects.equals(1, panel.getIsDmpPanel()) && panel.getDmpPanelList() != null) {
                hasDmp = true;
                break;
            }
        }
        return hasDmp;
    }

}


